/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @param {number} d
 * @return {number}
 */
export var findTheDistanceValue = function (arr1, arr2, d) {
  arr2.sort((a, b) => a - b)
  let ans = 0
  for (let i = 0; i < arr1.length; i++) {
    const low = arr1[i] - d
    const high = arr1[i] + d
    if (search(arr2, low, high)) ans++
  }
  return ans
}

var search = function (nums, low, high) {
  let l = 0,
    r = nums.length - 1
  while (l <= r) {
    const mid = l + Math.floor((r - l) / 2)
    if (nums[mid] >= low && nums[mid] <= high) return false
    if (nums[mid] < low) l = mid + 1
    else r = mid - 1
  }
  return true
}
